var log4js = require('log4js');
var logger = log4js.getLogger();
var waterfall = require('async-waterfall');
var EventProxy = require('eventproxy');
var _ = require('lodash');
var murmurhash = require('murmurhash');
var shortid = require('shortid');
var config = require('../../config.json');

var VIRTUAL_NODES = 100;

var con = [0,0,0,0,0];

var getServerMethod = function(node,call_back){
    var nodeHash = murmurhash.v3(node);
    waterfall([
        function(callback){
            getServersIP(function(err,servers){
                if(err){
                    call_back(err);
                }
                callback(null,servers);
            })
        },
        function(servers,callback){
            var virtualNodes = [];
            var ep = new EventProxy();
            ep.fail(function(err){
                call_back(err);
            });
            ep.after('virtual_node',servers.length,function(){
               var result =  _.sortBy(virtualNodes,function(n) {
                   return n.hash;
               });
                //console.log(result);
               callback(null,result);
            });
            servers.map(function(server){
                for (var i = 0; i < VIRTUAL_NODES; i++) {
                    var virtualNodeName = server + "&&VN" + i;
                    var hash = murmurhash.v3(virtualNodeName);
                    //console.log("虚拟节点[" + virtualNodeName + "]被添加, hash值为" + hash);
                    var node = {
                        server:server,
                        virtual:i,
                        hash:hash
                    }
                    virtualNodes.push(node);
                }
                ep.emit('virtual_node');
            })
        },
        function(virtualNodes,callback){
            //console.log(virtualNodes);
            while (nodeHash > virtualNodes[virtualNodes.length-1].hash) {
                nodeHash = nodeHash % virtualNodes[virtualNodes.length-1].hash;
            }
            var remainVirtualNodes =  _.dropWhile(virtualNodes, function(n) {
                return n.hash < nodeHash;
            });
            //console.log(remainVirtualNodes);
            if(remainVirtualNodes[0].server == "192.168.0.0:111"){
                con[0]++;
            } else if(remainVirtualNodes[0].server == "192.168.0.1:111") {
                con[1]++;
            }else if(remainVirtualNodes[0].server == "192.168.0.2:111") {
                con[2]++;
            }else if(remainVirtualNodes[0].server == "192.168.0.3:111") {
                con[3]++;
            }else if(remainVirtualNodes[0].server == "192.168.0.4:111") {
                con[4]++;
            }
                callback(null,remainVirtualNodes[0].server);
        }
    ],function(err,result){
        if(err){
            return call_back(err);
        }
        console.log(node+"-hash-"+nodeHash+"-server-"+result);
        return call_back(null,result);
    });
}


var ep = new EventProxy();

ep.after('con',1000,function(result){
    console.log(con);
    console.log(result);
});

for(var i=0;i<1000;i++){
    var node = shortid.generate();
    getServerMethod(node,function(err,result){
        ep.emit('con');
    });
}

var getServersIP = function(callback){
    callback(null,config.servers);
}